﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Windows;
using Silvermoon.Shapes;
using Silvermoon.OpenGL;
using Silvermoon.UI;
using Silvermoon.Animations;

namespace Silvermoon.Controls
{
    /// <summary>
    /// ListView item to be used in a group picker.
    /// </summary>
    public class GroupPickerItem : TextItem
    {
        static GroupPickerItem()
        {
            VisualStateManager manager = new VisualStateManager(
                new VisualStateGroup(
                    new VisualState(VState.Normal, 500),
                    new VisualState(VState.Pressed,
                    new ScaleTransition { To = new ScaleTransform(0.94f, 0.94f, 1f), Duration = 2 }
                    )
                 )
            );
            VisualStateManager.RegisterClassManager(typeof(GroupPickerItem), manager);
        }

        private RotateTransform pickerRotation;
        private ScaleTransform scaleTransform;

        private static PropertyAccessor<GroupPickerItem, float> RotationProperty = new PropertyAccessor<GroupPickerItem, float>("Rotation",
            c => c.Rotation,
            (c, v) => c.Rotation = v);

        private static PropertyAccessor<GroupPickerItem, float> ScaleFactorProperty = new PropertyAccessor<GroupPickerItem, float>("ScaleFactor",
            c => c.ScaleFactor,
            (c, v) => c.ScaleFactor = v);

        private static Margin padding = new Margin(16);

        public GroupPickerItem()
            : base()
        {
            Padding = padding;

            StringShape l = Label;
            l.Alignment = Alignment.Near;
            l.LineAlignment = Alignment.Far;
            pickerRotation = new RotateTransform { Axis = RotateAxis.XAxis, Angle = 0f };
            scaleTransform = new ScaleTransform();
            Transformation = pickerRotation;
            //Transformation += scaleTransform;
            AllowFlip = true;
            Margin = new Margin(6);
        }

        protected override void Dispose(bool dispose)
        {
            var w = GetWindow();
            if (w != null) w.Hiding -= Window_Hiding;

            base.Dispose(dispose);
        }

        /// <summary>
        /// Gets or sets whether the item rotates in and out when it becomes visible for the very first time and when the hosting window closes.
        /// </summary>
        public bool AllowFlip { get; set; }

        protected float Rotation
        {
            get { return pickerRotation.Angle; }
            set
            {
                if (pickerRotation.Angle != value)
                {
                    pickerRotation.Angle = value;
                    Invalidate(InvalidationFlag.Transformation);
                }
            }
        }

        private float scaleFactor = 1f;

        protected float ScaleFactor
        {
            get { return scaleFactor; }
            set
            {
                if (scaleFactor != value)
                {
                    scaleFactor = value;
                    float v = 1f - value;
                    float w = Width > 0 ? (float)Width : 1f;
                    float h = Height > 0 ? (float)Height : 1f;
                    scaleTransform.X = (1f / w - 1f) * v + 1f;
                    scaleTransform.Y = (1f / w - 1f) * v + 1f;
                    Invalidate(InvalidationFlag.Transformation);
                }
            }
        }




        protected override void OnInitialized()
        {
            base.OnInitialized();
            //if (Rotation != 0f)
            if (AllowFlip)
            {
                Window w = GetWindow();
                w.Hiding += new EventHandler(Window_Hiding);
                FloatTransition transition = new FloatTransition { Duration = 200, From = 70f, To = 0f, EasingFunction = LinearEase.Instance };
                RotationProperty.BeginAnimation(this, transition);

                //FloatTransition transition = new FloatTransition { Duration = 250, From = 0f, To = 1f, EasingFunction = PowerEase.Out };
                //ScaleFactorProperty.Animate(this, transition);

            }
        }

        void Window_Hiding(object sender, EventArgs e)
        {
            Window w = GetWindow();
            if (w != null) w.Hiding -= Window_Hiding;
            if (AllowFlip)
            {
                FloatTransition transition = new FloatTransition { Duration = 200, To = 70f, EasingFunction = LinearEase.Instance };
                RotationProperty.BeginAnimation(this, transition);
                //FloatTransition transition = new FloatTransition { Duration = 200, To = 0f, EasingFunction = PowerEase.In };
                //ScaleFactorProperty.Animate(this, transition);
            }
        }


        protected override void OnEnabledChanged()
        {
            base.OnEnabledChanged();
            Color = Enabled ? PhoneColors.Accent : PhoneColors.MediumDark;
            TextColor = Enabled ? PhoneColors.White : PhoneColors.Medium;
        }

        protected override Silvermoon.Shapes.Shape CreateDefaultBackground()
        {
            return PhoneColors.Accent;
        }

    }
}
